// Message dialog
msg = {
	Confirm : function(obj){
		ms = $(obj).attr('ms') ? $(obj).attr('ms') : 'Are you sure?';
		bootbox.confirm(ms, function(result) {
			if(result){
				window.location = $(obj).attr('url');
			}
		});
	},
	
	Alert : function(ms){
		bootbox.dialog({
			message : ms ? ms : '',
			buttons : {
				"button" : {
					"label" : "Ok",
					"className" : "btn btn-primary",
					"callback": function() {}
				}
			}
		});
	}
}

loading = {
	Show : function (path){
		jQuery('body').append('<div id="page-loading"><img src="' + path + '/loading.gif" alt="Loading..." /></div>');
	},
	
	Hide : function (){
		jQuery('#page-loading').remove();
	}
}

//override dialog's title function to allow for HTML titles
$.widget("ui.dialog", $.extend({}, $.ui.dialog.prototype, {
  _title: function(title) {
    var $title = this.options.title || '&nbsp;'
    if( ("title_html" in this.options) && this.options.title_html == true )
      title.html($title);
    else title.text($title);
  }
}));

dialog = {
	delete : function(obj){
		var that = $(obj);
		var msg = that.attr('msg') || 'Are you sure?',
				title = that.attr('title') || 'Confirm';

		var box = '<div id="dialog-delete-confirm" class="hide">'
							+ '<p class="bigger-110 bolder grey">'
						    + '<i class="icon-hand-right blue bigger-120"></i>'
						    + msg
						  + '</p>'
							+'</div>';
		$('#dialog-delete-confirm').remove();
		$('body').append(box);
		
	  $( "#dialog-delete-confirm" ).removeClass('hide').dialog({
	    resizable: false,
	    modal: true,
	    title: "<div class='widget-header'><h4 class='smaller'><i class='icon-warning-sign red'></i> "+title+"</h4></div>",
	    title_html: true,
	    buttons: [
	      {
	        html: "<i class='icon-trash bigger-110'></i>&nbsp; Delete",
	        "class" : "btn btn-danger btn-xs",
	        click: function() {
	          window.location = that.attr('href');
	        }
	      }
	      ,
	      {
	        html: "<i class='icon-remove bigger-110'></i>&nbsp; Cancel",
	        "class" : "btn btn-xs",
	        click: function() {
	          $( this ).dialog( "close" );
	        }
	      }
	    ]
	  });
	}
}

// dialog
$(function(){
	$('a.dialog-delete').on('click', function(e) {
	  e.preventDefault();
	  dialog.delete(this);
	});
});
